#!/bin/sh
#
# Script args:
# $0: full path to script
# $1: full path to top level package dir, no trailing slash
# $2: full path to installed location
# $3:  ????? Path to install Volume????
# $4:  ?????

update_extension_10_6()
{	
    NEW_KEXTDIR="HuaweiDataCardDriver.kext"
    NEW_KEXTNAME="HuaweiDataCardDriver"
    NEW_APP="HWActivateApp"
    TEST_COMM="HUAWEIMobile-Pcui"
    if [ -d /System/Library/Extensions/MBBDataCardDriver.kext ] 
    then
        NEW_KEXTDIR="MBBDataCardDriver.kext"
        NEW_KEXTNAME="MBBDataCardDriver"
        NEW_APP="MBBActivateApp"
        TEST_COMM="MBBMobile-Pcui"
    fi

    if ls /dev/tty.* |grep -c $TEST_COMM;then
        echo "$TEST_COMM is ok"
    else
	    if kextstat|grep AppleUSBCDC;then
            kextunload /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBCDC.kext
            /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/"$NEW_APP"&
            kextload /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBCDC.kext
        else
            /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/"$NEW_APP"&
        fi

    fi
	
    
		
	if kextstat |grep -c "$NEW_KEXTNAME"; then
		echo "$NEW_KEXTNAME has been loaded!"
	else
		sleep 20
		echo "Load "$NEW_KEXTDIR" begin"
		kextload  /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/*.kext
		echo "Load "$NEW_KEXTDIR" end"
	fi	
	

	PID=`ps -axc | grep kextd | perl -e '($a,$r,$z)=split /  */, <>; print $r;'`
    kill -HUP $PID
    touch /System/Library/Extensions


}

update_extension()
{
    NEW_KEXTDIR="HuaweiDataCardDriver.kext"
    NEW_APP="HWActivateApp"
    TEST_COMM="HUAWEIMobile-Pcui"
    if [ -d /System/Library/Extensions/MBBDataCardDriver.kext ]
    then
        NEW_KEXTDIR="MBBDataCardDriver.kext"
        NEW_APP="MBBActivateApp"
        TEST_COMM="MBBMobile-Pcui"
    fi

    kextload -t /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/*.kext
    PID=`ps -axc | grep kextd | perl -e '($a,$r,$z)=split /  */, <>; print $r;'`
    kill -HUP $PID
    kextload -t /System/Library/Extensions/"$NEW_KEXTDIR"/

    if ls /dev/tty.* |grep -c $TEST_COMM;then
        echo "$TEST_COMM is ok"
    else
        if kextstat|grep AppleUSBCDC;then
            kextunload /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBCDC.kext
            /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/"$NEW_APP"
            kextload /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBCDC.kext
        else
            /System/Library/Extensions/"$NEW_KEXTDIR"/Contents/PlugIns/"$NEW_APP"
        fi

        if sw_vers -productVersion |grep -c 10.4; 
		then
            if kextstat|grep AppleUSBCDCDMM.kext;
			then
                kextunload /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns/AppleUSBCDCDMM.kext
            fi
        fi
    fi

    if sw_vers -productVersion |grep -c 10.5; then
        touch /System/Library/Extensions
    fi
    if sw_vers -productVersion |grep -c 10.6; then
    touch /System/Library/Extensions
    fi
}

update_driver()
{
    echo "path is :"
    NEW_MobileConnect="HUAWEI Mobile Connect - 3G Modem"
    OLD_MobileConnect="HUAWEI Mobile Connect - 3G Modem"
    NEW_MobileCCL="Huawei Mobile.ccl"
    OLD_MobileCCL="Huawei Mobile.ccl"
    NEW_KEXTDIR="HuaweiDataCardDriver.kext"
    OLD_KEXTDIR="HuaweiDataCardDriver.kext"
    NEW_PATCH="USBExpressCardCantWake_Huawei.kext"
    OLD_PATCH="USBExpressCardCantWake_Huawei.kext"
    if [ -d /System/Library/MBBDataCardDriver.kext ]
    then
        NEW_MobileConnect="MBB Mobile Connect - 3G Modem"
        NEW_MobileCCL="MBB Mobile.ccl"
        NEW_KEXTDIR="MBBDataCardDriver.kext"
        NEW_PATCH="USBExpressCardCantWake_MBB.kext"
    fi
    if [ -d /System/Library/Extensions/MBBDataCardDriver.kext ]
    then
        OLD_MobileConnect="MBB Mobile Connect - 3G Modem"
        OLD_MobileCCL="MBB Mobile.ccl"
        OLD_KEXTDIR="MBBDataCardDriver.kext"
        OLD_PATCH="USBExpressCardCantWake_MBB.kext"
    fi
    echo $1
	if [ -f "$1/Contents/Resources/$NEW_MobileConnect" ]
	then
        rm -Rf "/System/Library/Modem Scripts/$OLD_MobileConnect"
        cp -f "$1/Contents/Resources/$NEW_MobileConnect" "/System/Library/Modem Scripts/$NEW_MobileConnect"
	fi
    if sw_vers -productVersion |grep -c 10.5; 
    then
	    if [ -d "$1/Contents/Resources/$NEW_MobileCCL" ]
            then
                rm -Rf "/System/Library/Modem Scripts/$OLD_MobileCCL"
                cp -Rf "$1/Contents/Resources/$NEW_MobileCCL" "/System/Library/Modem Scripts/"
	    fi
    fi
    if [ -f "$1/Contents/Resources/$NEW_MobileConnect" ]
	then
        rm -Rf "/Library/Modem Scripts/$OLD_MobileConnect"
        cp -f "$1/Contents/Resources/$NEW_MobileConnect" "/Library/Modem Scripts/$NEW_MobileConnect"
    fi


    rm -Rf /System/Library/Extensions/"$OLD_KEXTDIR"
    mv -f /System/Library/"$NEW_KEXTDIR" /System/Library/Extensions/"$NEW_KEXTDIR"

    rm -Rf /System/Library/Extensions/"$OLD_PATCH"
    mv -f /System/Library/"$NEW_PATCH" /System/Library/Extensions/"$NEW_PATCH"

    chown -R root:wheel /System/Library/Extensions/"$NEW_KEXTDIR"
    chmod -R 755 /System/Library/Extensions/"$NEW_KEXTDIR"

    chown -R root:wheel /System/Library/Extensions/"$NEW_PATCH"
    chmod -R 755 /System/Library/Extensions/"$NEW_PATCH"
    rm /System/Library/Extensions.kextcache
	
    rm -Rf /System/Library/Caches				   
    touch /System/Library/Extensions

    if sw_vers -productVersion |grep -c 10.6; then
        update_extension_10_6
    else
        update_extension
    fi
}



install_driver()
{
    OLD_FILE="/System/Library/Extensions/HuaweiDataCardDriver.kext/Contents/Info.plist"
    if [ -d /System/Library/Extensions/MBBDataCardDriver.kext ]
    then
        OLD_FILE="/System/Library/Extensions/MBBDataCardDriver.kext/Contents/Info.plist"
    fi

    NEW_FILE="/System/Library/HuaweiDataCardDriver.kext/Contents/Info.plist"
    if [ -d /System/Library/MBBDataCardDriver.kext ]
    then
        NEW_FILE="/System/Library/MBBDataCardDriver.kext/Contents/Info.plist"
    fi    
    VERSIONVOL=3


    OLD_VERSIONLINE=$[ $(cat -n $OLD_FILE |grep "CFBundleShortVersionString" |cut -d \< -f1)+1 ]
    NEW_VERSIONLINE=$[ $(cat -n $NEW_FILE |grep "CFBundleShortVersionString" |cut -d \< -f1)+1 ]



    #if [ "$(ls /System/Library/Extensions/ |grep HuaweiDataCardDriver)" != ""] 
    if [ -f "$OLD_FILE" ]
    then
        NEED_UPDATE=0
        EXT_VERSION=$(cat -n $OLD_FILE |head -n 20 |grep $OLD_VERSIONLINE |cut -f $VERSIONVOL |cut -d \< -f2 |cut -d \> -f2)
        VERSION=$(cat -n $NEW_FILE |head -n 20 |grep $NEW_VERSIONLINE |cut -f $VERSIONVOL |cut -d \< -f2 |cut -d \> -f2)
		
		
        EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $4}'` 
        if [ "$(echo $EXT_TEMP)" != "" ]
        then
            TEMP=`echo $VERSION |awk -F '.' '{print $4}'`
	    COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
	    if [ $COMPARE = "0" ] 
	    then
	        EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $1}'`
	        TEMP=`echo $VERSION |awk -F '.' '{print $1}'`
                COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
                if [ $COMPARE -lt "0" ]
		then
		    echo "COMPARE < 0" #packet is old
		    NEED_UPDATE=0
	        else
		    if [ $COMPARE = "0" ]
		    then
			EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $2}'`
		        TEMP=`echo $VERSION |awk -F '.' '{print $2}'`
                        COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
			if [ $COMPARE -lt "0" ]
			then
			    NEED_UPDATE=0 #packet is old
			else
			    if [ $COMPARE = "0" ]
			    then
			        EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $3}'`
		                TEMP=`echo $VERSION |awk -F '.' '{print $3}'`
                                COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
				if [ $COMPARE -lt "0" ]
				then
				    NEED_UPDATE=0 #packet is old
				else
				    NEED_UPDATE=1
				fi
		            else
			         NEED_UPDATE=1
	                    fi
			fi
		    else
		        NEED_UPDATE=1
		    fi
	        fi
	    else
	        NEED_UPDATE=1
            fi
        else
            NEED_UPDATE=1
        fi


        if [ $NEED_UPDATE = "0" ]
        then
            if [ -d /System/Library/MBBDataCardDriver.kext ]
            then
                rm -Rf /System/Library/MBBDataCardDriver.kext
            else
                if [ -d /System/Library/HuaweiDataCardDriver.kext ]
                then
                    rm -Rf /System/Library/HuaweiDataCardDriver.kext
                fi
            fi

            if [ -d /System/Library/USBExpressCardCantWake_MBB.kext ]
            then
                rm -Rf /System/Library/USBExpressCardCantWake_MBB.kext
            else
                if [ -d /System/Library/USBExpressCardCantWake_Huawei.kext ]
                then
                    rm -Rf /System/Library/USBExpressCardCantWake_Huawei.kext
                fi
            fi
        else
            update_driver "$1"
        fi
    else
        update_driver "$1"
    fi
}
install_driver "$1"
exit 0
